<?php
	
	include('../classes/clsDB.php');
	
	include('../config.inc.php');
	$r=new ReturnRecord();
	
	
	$SQL="SELECT SCommission,SaleType FROM affiliatePrograms,affiliateJoinedPrograms WHERE affiliatePrograms.id=$_GET[ap] AND affiliateJoinedPrograms.affiliateID=$_COOKIE[AffCode] AND affiliatePrograms.id=affiliateJoinedPrograms.affiliateProgramsID";
	//print $SQL;
	$sq2=$r->rawQuery($SQL);  
	$myrow = mysql_fetch_row($sq2);
	
	
	if($myrow[0]>0){
		if($myrow[1]=="$"){
			$CAmount=$myrow[0];
		}else{
			$CAmount=$_GET['sa']*($myrow[0]/100);
		}
		//$SQL="SELECT COUNT(*) FROM affiliateSales WHERE VisitsID='$_COOKIE[AffSid]' ";
		//$sq2=$r->rawQuery($SQL); 
		//$data=mysql_fetch_array($sq2);
		//if($data[0]==0){ 
			$SQL="INSERT INTO affiliateEarn (affiliateProgramsID,affiliateID,CommissionAmount,VisitsID,EDateTime,EType,SaleAmount) VALUES ($_GET[ap],$_COOKIE[AffCode],$CAmount,$_COOKIE[AffSid],NOW(),'Sale',$_GET[sa]) ";
			$sq2=$r->rawQuery($SQL);
			$SQL="UPDATE affiliate SET Account=Account+$CAmount WHERE id=$_COOKIE[AffCode]";
			$sq2=$r->rawQuery($SQL);
		//}  
	}
	
?>